import 'package:flutter/material.dart';
import 'package:example_flutter/blocs/chuck_category_bloc.dart';
import 'package:example_flutter/chuck_joke_view.dart';
import 'package:example_flutter/common_views.dart';
import 'package:example_flutter/models/chuck_categories.dart';
import 'package:example_flutter/networking/response.dart';

class GetChuckCategories extends StatefulWidget {
  GetChuckCategories({Key key}) : super(key: key);

  @override
  _GetChuckCategoriesState createState() => _GetChuckCategoriesState();
}

class _GetChuckCategoriesState extends State<GetChuckCategories> {

  ChuckCategoryBloc _bloc;

  @override
  void initState() {
    _bloc = ChuckCategoryBloc();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        automaticallyImplyLeading: false,
        title: Text(
          'Chuck Categories',
          style: TextStyle(color: Colors.white, fontSize: 20),
        ),
        backgroundColor: Color(0xFF333333),
      ),
      backgroundColor: Color(0xFF333333),
      body: RefreshIndicator(
        onRefresh: () => _bloc.fetchCategories(),
        child: StreamBuilder<Response<ChuckCategories>>(
          stream: _bloc.chuckListStream,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              switch (snapshot.data.status) {
                case Status.LOADING:
                  return Loading(loadingMessage: snapshot.data.message);
                  break;
                case Status.COMPLETED:
                  return CategoryList(categoryList: snapshot.data.data);
                  break;
                case Status.ERROR:
                  return Error(
                    errorMessage: snapshot.data.message,
                    onRetryPressed: () => _bloc.fetchCategories(),
                  );
                  break;
              }
            }
            return Center(
              child: RaisedButton(
                color: Colors.white,
                child: Text('重新获取数据', style: TextStyle(color: Colors.black)),
                onPressed: () {
                  _bloc.fetchCategories();
                }),
            );
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    _bloc.dispose();
    super.dispose();
  }
}

class CategoryList extends StatelessWidget {

  final ChuckCategories categoryList;

  const CategoryList({Key key, this.categoryList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF202020),
      body: ListView.builder(
        itemBuilder: (context, index) {
          return Padding(
            padding: EdgeInsets.symmetric(
              horizontal: 0,
              vertical: 1
            ),
            child: InkWell(
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => ShowChuckyJoke(categoryList.categories[index])));
              },
              child: SizedBox(
                height: 65,
                child: Container(
                  color: Color(0xFF333333),
                  alignment: Alignment.center,
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(4, 0, 0, 0),
                    child: Text(
                      categoryList.categories[index],
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20,
                        fontWeight: FontWeight.w100,
                        fontFamily: 'Roboto'
                      ),
                      textAlign: TextAlign.left,
                    ),
                  ),
                ),
              ),
            ),
          );
        },
        itemCount: categoryList.categories.length,
        shrinkWrap: true,
        physics: ClampingScrollPhysics(),
      ),
    );
  }
}